/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.mol.market.Constants;
import com.mol.market.Session;
import com.mol.market.common.util.AlarmManageUtils.PushRules;
import com.mol.market.ui.HomeTabActivity;
import com.mol.market.ui_v4.AppManagerActivity;

/**
 * 循环广播检测通知栏推荐功能
 * 
 * @author wufenglong
 * @date 2011-10-14
 * @since Version 0.8.0
 * */
public class AppPushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Session session = Session.get(context);

        String action = intent.getAction();

        if (Constants.BROADCAST_CLICK_INTENT.equals(action)) {

            Intent i = new Intent(context, AppManagerActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            
        } else {
            if (session.isNotificationRecommendApps()) {
                AlarmManageUtils.notifyPushService(context, false);
            }
            //重置Push信息
//            resetPushInformation(session.getUserLastLoginTime(),session.isSecondLogin(),context);
        }
        
    }
    
    /**
     * 重置push信息
     * @param lastLoginTime
     * @param firstLogin
     * @param mContext
     */
    private void resetPushInformation(long lastLoginTime,boolean secondLogin, Context mContext){
        //手机关机时间
        long pastTime = System.currentTimeMillis() - lastLoginTime;
        
        if(pastTime < 0){
            //系统异常，发生了未知错误
            return ;
        }
        
        //根据业务逻辑进行设置
        if (!secondLogin) {
            AlarmManageUtils.notifyCallbackService(mContext,PushRules.NOLOGIN_NEW_USER_3_DAY, pastTime);
        } else {
            AlarmManageUtils.notifyCallbackService(mContext,PushRules.NOLOGIN_SILENCE_USER_7_DAY, pastTime);
        }
        
    }
}
